package com.neusoft.yuejian12.yuejian_12.fragment;


import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.neusoft.yuejian12.yuejian_12.R;
import com.neusoft.yuejian12.yuejian_12.adapter.ProConsultListAdapter;
import com.neusoft.yuejian12.yuejian_12.javabean.ProBean;
import com.neusoft.yuejian12.yuejian_12.listener.RetrofitListener;
import com.neusoft.yuejian12.yuejian_12.model.ProModel;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * A simple {@link Fragment} subclass.
 */
public class ProConsultFragment extends Fragment implements RetrofitListener<List<ProBean>> {

    private RecyclerView recyclerView;
    private List mDataList;
    private View view = null;
    private int pmId;
    private int u_id;

    private EditText addCon;
    private Button putadd;
    private String consultinfo;

    private ProConsultListAdapter proConsultListAdapter;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        view =  inflater.inflate(R.layout.fragment_proconsult, container, false);
        initViews();
        initEvent();
        getData();
        return view;
    }
    private void initViews(){
        recyclerView = (RecyclerView) view.findViewById(R.id.rV_ProConsult);
        recyclerView.setLayoutManager(new LinearLayoutManager(this.getActivity()));
        recyclerView.setItemAnimator(new DefaultItemAnimator());

        addCon = (EditText) view.findViewById(R.id.consultinfo);
        putadd = (Button) view.findViewById(R.id.PutConsult);

    }
    private void initEvent(){
        putadd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                SharedPreferences sp = getActivity().getSharedPreferences("userInfo", Context.MODE_PRIVATE);
                u_id = sp.getInt("u_id", 0);
                consultinfo = addCon.getText().toString();

                Bundle bundle=getArguments();
                int pmId=bundle.getInt("pm_id");

                ProModel proModel= new ProModel();
                //发送请求
                Call<ProBean> call = proModel.consultPro
                        (pmId,u_id, consultinfo);

                //2.处理结果
                call.enqueue(new Callback<ProBean>() {
                    @Override
                    public void onResponse(Call<ProBean> call, Response<ProBean> response) {
                        ProBean data = (ProBean) response.body();  //获取数据
                        if (data.getSuccess().equals("1")) {
                            Toast.makeText(ProConsultFragment.this.getActivity(), "添加成功", Toast.LENGTH_LONG).show();

//                            Intent intent = new Intent();
//                            intent.setClass(ProConsultFragment.this.getActivity(), ProDetailActivity.class);
//                            startActivity(intent);

                        } else {
                            Toast.makeText(ProConsultFragment.this.getActivity(), "添加失败", Toast.LENGTH_LONG).show();

                        }
                    }

                    @Override
                    public void onFailure(Call<ProBean> call, Throwable t) {

                    }
                });

            }

        });
    }







    private void getData(){
        Bundle bundle=getArguments();
        int pmId=bundle.getInt("pm_id");
        ProModel proModel = new ProModel();
        proModel.consultList(pmId,this);

    }

    @Override
    public void onSuccess(List<ProBean> proBeen) {
        proConsultListAdapter = new ProConsultListAdapter(ProConsultFragment.this.getActivity(),proBeen);
        recyclerView.setAdapter(proConsultListAdapter);
    }

    @Override
    public void onFail() {

    }
}
